namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserCustomerSetPage2 : Page
    {
        protected DropDownList AreaDdl;
        protected Button cancelBtn;
        protected DropDownList catDdl;
        protected Image iCustomerInfo;
        protected CheckBox IFChk;
        protected Label lbArea;
        protected Label lbCat;
        protected Label lbfrom;
        protected Label lbFromId;
        protected Label lbProvince;
        protected Label lbTo;
        protected Label lbToId;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected Button SearchBtn;
        protected Button transferBtn;
        protected DataGrid UserListDg;
        protected TextBox userNameTxb;

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.lbfrom.Text = string.Empty;
            this.lbTo.Text = string.Empty;
            this.lbFromId.Text = string.Empty;
            this.lbToId.Text = string.Empty;
        }

        private void InitializeComponent()
        {
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.transferBtn.Click += new EventHandler(this.transferBtn_Click);
            this.cancelBtn.Click += new EventHandler(this.cancelBtn_Click);
            this.UserListDg.ItemCommand += new DataGridCommandEventHandler(this.UserListDg_ItemCommand);
            this.UserListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.UserListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
                this.SetFrom();
                this.UserListDgDataBind();
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.UserListDg.CurrentPageIndex = 0;
            this.UserListDgDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbArea", language, path);
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "lbCat", language, path);
            this.SearchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("UserCustomerSetPage", "SearchBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSetting.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerOwershipSettingEn.jpg";
                    break;
            }
        }

        private void transferBtn_Click(object sender, EventArgs e)
        {
            if (this.IFChk.Checked)
            {
                base.Response.Write("<script>window.open('UserCustomerSetPage4.aspx?FromId=" + this.lbFromId.Text + "&ToId=" + this.lbToId.Text + "');</script>");
            }
            else
            {
                base.Response.Write("<script>window.open('UserCustomerSetPage3.aspx?FromId=" + this.lbFromId.Text + "&ToId=" + this.lbToId.Text + "');</script>");
            }
        }

        private void UserListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            string strUserId = e.CommandArgument.ToString();
            if (((str = cmd) != null) && (string.IsInterned(str) == "Edit"))
            {
                SysUser user = ManageUser.GetUserViaId(new Guid(strUserId));
                if (this.lbfrom.Text == "")
                {
                    this.lbfrom.Text = user.LoginName;
                    this.lbFromId.Text = user.UserId.ToString();
                }
                else
                {
                    this.lbTo.Text = user.LoginName;
                    this.lbToId.Text = user.UserId.ToString();
                }
            }
        }

        private void UserListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            try
            {
                this.UserListDg.CurrentPageIndex = e.NewPageIndex;
                this.UserListDgDataBind();
            }
            catch (Exception)
            {
            }
        }

        private void UserListDgDataBind()
        {
            this.UserListDg.DataSource = ManageUser.GetUserWithDataSet(new Guid(this.AreaDdl.SelectedValue), this.provinceDdl.SelectedValue, this.catDdl.SelectedValue, "All", this.userNameTxb.Text.Trim());
            this.UserListDg.DataBind();
        }
    }
}

